package com.school.service.impl;

import com.school.entity.CertificateHonor;
import com.school.repository.CertificateHonorRepository;
import com.school.service.CertificateHonorService;
import com.school.untils.AuthMDA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CertificateHonorServiceImp implements CertificateHonorService {
    @Autowired
    CertificateHonorRepository certificateHonorRepository;
    @Value("${web.upload.path}")
    String basePath;

    /**
     * 增加
     * @param certificateHonor
     * @return
     */
    @Override
    public CertificateHonor addCertificateHonor(List<MultipartFile> fileList, CertificateHonor certificateHonor) throws IOException {
        List<String> filesList = AuthMDA.setFiles(fileList,basePath);
        certificateHonor.setFiles(filesList.toString());
        certificateHonor.setCreateDate(new Date());
        certificateHonor.setUpdateDate(new Date());
        return certificateHonorRepository.save(certificateHonor);
    }

    /**
     * 修改
     * @param certificateHonor
     * @return
     */
    @Override
    public CertificateHonor updateCertificateHonor(CertificateHonor certificateHonor) {
        certificateHonor.setUpdateDate(new Date());
        return certificateHonorRepository.saveAndFlush(certificateHonor);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public CertificateHonor getDetailsById(String id) {
        return certificateHonorRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteCertificateHonor(String id) {
        CertificateHonor certificateHonor = null;
        try {
            certificateHonor = certificateHonorRepository.findById(Integer.valueOf(id)).get();
            certificateHonorRepository.delete(certificateHonor);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除
     * @param idList
     */
    @Override
    public void deleteCertificateHonorList(String idList) {
        String[] split = idList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        certificateHonorRepository.deleteCertificateHonorsByIdIn(list);
    }

    /**
     * 根据条件分页查询
     * @param certificateHonor,page,size
     */
    @Override
    public Page<CertificateHonor> specificPageQueryCertificateHonorList(CertificateHonor certificateHonor,int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(certificateHonor != null)
                {
                    String issuerAuthority = certificateHonor.getIssuerAuthority();
                    String type = certificateHonor.getType();
                    String name = certificateHonor.getName();
                    if (!StringUtils.isEmpty(issuerAuthority))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("issuerAuthority").as(String.class), issuerAuthority));
                    }
                    if (!StringUtils.isEmpty(type))
                    {
                        predicates.add(criteriaBuilder.equal(root.get("type").as(String.class),type));
                    }
                    if (!StringUtils.isEmpty(name))
                    {
                        predicates.add(criteriaBuilder.like(root.get("name").as(String.class),'%' + name + '%'));
                    }
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC,"updateDate"));
        return certificateHonorRepository.findAll(querySpecification,pageRequest);
    }

}
